package com.litidea.filters;

import static com.litidea.commons.tools.ConvertPassword.getMyPassword;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.litidea.dao.SystemDAO;
import com.litidea.dataobjects.UserDO;
import com.litidea.dataobjects.UserLoginDO;

public class FilterProcessor {
	
	private Log logger = LogFactory.getLog(FilterProcessor.class);
	
	public final static Integer USER_LOGIN_OUT = 1;
	
	public final static Integer TIMEOUT_LOGIN_OUT = 0;
	
	private SystemDAO systemDAO;
	
	private Map<String , UserLoginDO> user_key_value = new ConcurrentHashMap<String , UserLoginDO>(256);
	
	private Map<Integer , String> user_id_key = new ConcurrentHashMap<Integer , String>(256);

	public FilterProcessor(SystemDAO systemDAO) {
		this.systemDAO = systemDAO;
	}
	//根据登录名获取用户信息
	public UserDO getUserByLoginName(String loginName) {
		return systemDAO.getUserByLoginName(loginName);
	}
	
	/**
	 * 校验密码信息
	 * @param inputPassword
	 * @param userDO
	 * @return
	 */
	public static boolean checkPassword(String inputPassword , UserDO userDO) {
		String password = getMyPassword(inputPassword);
		return password.equals(userDO.getUserPass());
	}
	
	/**
     * 获取内存用的基本信息
     * @param loginConvertName
     * @return
     */
    public UserLoginDO getMemUser(String loginConvertName) {
    	UserLoginDO userLoginDO = user_key_value.get(loginConvertName);
    	if(userLoginDO != null) {
    		UserLoginDO tmpLoginDO = new UserLoginDO();
    		try {
				BeanUtils.copyProperties(tmpLoginDO, userLoginDO);
			} catch (Exception e) {
				logger.error("when copy user info error" , e);
			}
			return tmpLoginDO;
    	}
    	return null;
    }
    
    /**
     * 设置到cache中
     * @param key
     * @param userDO
     */
    public boolean addUserCache(String key , UserDO userDO , String ip) {
    	Integer userId = userDO.getId();
    	String tempKey = user_id_key.get(userId);
    	if(tempKey != null) {
    		loginOut(tempKey , USER_LOGIN_OUT);
    	}
     	UserLoginDO userLoginDO = new UserLoginDO();
    	try {
    		BeanUtils.copyProperties(userLoginDO, userDO);
    		Integer id = systemDAO.addLogin(userId, ip);
    		userLoginDO.setLoginId(id);
    		userLoginDO.setLastOptionTime(System.currentTimeMillis());
    		user_key_value.put(key, userLoginDO);
    		user_id_key.put(userId, key);
    		return true;
    	}catch(Exception e) {
    		return false;
    	}
    }
    
    /**
     * 删除缓存内容
     * @param key
     */
    public void loginOut(String key , Integer type) {
    	UserLoginDO userLoginDO = user_key_value.remove(key);
    	if(userLoginDO != null) {
    		Integer userId = userLoginDO.getId();
    		user_id_key.remove(userId);
    		systemDAO.loginOut(userLoginDO.getLoginId(), type);
    	}
    }
    
    /**
     * 检查缓存是否过期
     * @param loginConvertName
     * @return
     */
    public boolean isExpired(String loginConvertName) {
    	UserLoginDO userLoginDO = user_key_value.get(loginConvertName);
    	if(userLoginDO == null) return true;
    	return userLoginDO.isExpired();
    }
}
